﻿using System;
using System.Data;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using PowerToolsWeb.DataAccess;
using PowerToolsWeb.Facade;

namespace PowerToolsWeb.Admin
{
    public partial class ProductList : AdminPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDDL();
                BindProduct();
            }
        }
        void BindDDL()
        {
            Tpt_ProducttypeCollection typeList = new Tpt_ProducttypeCollection();
            typeList.ListAll();
            ListItem item = new ListItem("所有类别", "-1");
            this.ddlType.Items.Insert(0, item);
            InitDDl(typeList.DataTable.DefaultView, ddlType, 0, 0);
        }
        //递归DDL
        private void InitDDl(DataView dv, DropDownList ddl, int parentId, int level)
        {
            ListItem ListItem1;
            dv.RowFilter = "PARANTTYPEID=" + parentId;
            foreach (DataRowView drv in dv)
            {
                string empty = "";
                for (int i = 0; i < level; i++)
                {
                    empty = empty + "　　";
                }
                ListItem1 = new ListItem();
                ListItem1.Text = empty + drv["TYPENAME"].ToString();
                ListItem1.Value = drv["TYPEID"].ToString();
                ddl.Items.Add(ListItem1);
                this.InitDDl(dv, ddl, int.Parse(ListItem1.Value), level + 1);
            }
        }
        void BindProduct()
        {
            Tpt_ProductCollection product = new Tpt_ProductCollection();
            product.ListProduct(int.Parse(ddlType.SelectedValue), txbProductName.Text.Trim());
            this.GridView1.DataSource = product.DataTable;
            this.GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            BindProduct();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {
                if (e.Row.Cells[3].Text.Trim() == "1")
                    e.Row.Cells[3].Text = "是";
                else
                    e.Row.Cells[3].Text = "否";
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E8E6EA'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            BindProduct();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteProduct")
            {
                Tpt_Product product = new Tpt_Product();
                if (!product.SelectByPk(int.Parse(e.CommandArgument.ToString())))
                {
                    Alert(Page, "商品信息有误，删除失败");
                    return;
                }
                if (!product.Delete())
                {
                    Alert(Page, "删除商品失败");
                    return;
                }
                //删除商品图片
                DeleteImage(product.PICURL);
                Alert(Page, "删除商品成功");

                BindProduct();
            }
        }
        //删除上传的文件
        bool DeleteImage(string picURL)
        {
            try
            {
                if (Common.StaticClass.FileExists(Page.Server.MapPath("/") + picURL))
                    File.Delete(Page.Server.MapPath("/") + picURL);
            }
            catch (Exception e)
            {
                Alert(Page, "删除图片失败！原因：" + e.Message);
                return false;
            }
            return true;
        }
    }
}
